package com.woniuxy.joylife.manager.controller;

import com.woniuxy.common.constant.ManagerConstant;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.common.validate.AddGroup;
import com.woniuxy.common.validate.UpdateGroup;
import com.woniuxy.joylife.manager.entity.ManagerEntity;
import com.woniuxy.joylife.manager.service.ManagerService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 物业信息
 *
 * @author renxiang
 * @email 981405195@qq.com
 * @date 2020-05-14 10:57:33
 */
@RestController
@RequestMapping("manager/manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:manager:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = managerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{managerId}")
    //@RequiresPermissions("manager:manager:info")
    public R info(@PathVariable("managerId") String managerId){
		ManagerEntity manager = managerService.getById(managerId);

        return R.ok().put("manager", manager);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:manager:save")
    public R save(@Validated({AddGroup.class}) @RequestBody ManagerEntity manager){
        manager.setManagerId(KeyUtil.generateKey());
        manager.setCreatetime(new Date());
        manager.setState(ManagerConstant.UNDELETED);
		managerService.save(manager);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:manager:update")
    public R update(@Validated({UpdateGroup.class})@RequestBody ManagerEntity manager){
        manager.setUpdatetime(new Date());
		managerService.updateById(manager);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:manager:delete")
    public R delete(@RequestBody String[] managerIds){
		managerService.removeByIds(Arrays.asList(managerIds));

        return R.ok();
    }

}
